package com.egene.tw.activity.wiki;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.api.EgeneApi;
import com.egene.tw.base.BaseActivity;
import com.egene.tw.model.WikiArticle;
import com.egene.tw.model.net.ResponseBean;
import com.egene.tw.utils.ImageUtil;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.egene.tw.utils.TitleUtil;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

@EActivity(R.layout.activity_wik_info)
public class WikInfoActivity extends BaseActivity {
    public static final String ID = "id";
    //ToolBar Views
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolBarTitle;


    @ViewById
    ImageView ivImg;
    @ViewById
    TextView tvContent;
    @ViewById
    WebView webView;

    @AfterViews
    void initViews() {
        initToolBar();
    }

    private void initToolBar() {
        mToolBarTitle.setText(R.string.detail);
        mToolbarView.setNavigationIcon(R.drawable.ic_back);
        mToolbarView.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @AfterInject
    void deal() {
        String id = getIntent().getStringExtra(ID);
        EgeneApi.getWikiArticleInfo(id, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                ResponseBean<WikiArticle> result = JsonUtil.toBean(responseBody,
                        new TypeToken<ResponseBean<WikiArticle>>() {
                        }.getType());
                if (result != null) {
                    if (result.isSuccess()) {
                        updateUI(result.getData());
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }
        });
    }

    private void updateUI(WikiArticle article) {
        String img = article.getImg_url();
//        String img = "http://a.hiphotos.baidu.com/image/pic/item/0824ab18972bd40725d52a107e899e510eb309c8.jpg";// article.getImg_url();
        if (TextUtils.isEmpty(img)) {
            img = "123";
        }
        Picasso.with(this).load(img).placeholder(R.drawable.art_default).error(R.drawable.art_default).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                int height = bitmap.getHeight();
                int width = bitmap.getWidth();

                int imageViewWidth = ivImg.getWidth();

                int imageViewHeight = imageViewWidth * height / width;
                ivImg.setImageBitmap(bitmap);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(imageViewWidth, imageViewHeight);
                ivImg.setLayoutParams(layoutParams);
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
                ivImg.setImageDrawable(errorDrawable);
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
                ivImg.setImageDrawable(placeHolderDrawable);
            }
        });

//        if (!TextUtils.isEmpty(article.getImg_url())) {
//        ImageUtil.loadImage(this, ivImg, article.getImg_url());
//        }
//
//        if(TextUtils.isEmpty(article.getImg_url())){
//            Picasso.with(this).load(R.drawable.pic_product_defualt).into(ivImg);
//        }else{
//            Picasso.with(this).load(EgeneApi.BASE_URL + article.getImg_url()).placeholder(R.drawable.pic_product_defualt).into(ivImg);
//        }
        mToolBarTitle.setText(TitleUtil.getTitle(article.getTitle()));
//        tvContent.setText(Html.fromHtml(article.getContent()));
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.loadData(article.getContent(), "text/html; charset=UTF-8", "UTF-8");
    }
}
